package com.example.pre.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.pre.business.AskOffBusiness;
import com.example.pre.controller.base.ControllerBase;
import com.example.pre.enums.AskOffTypeEnum;
import com.example.pre.exception.ExceptionMessage;
import com.example.pre.exception.ExtParaException;
import com.example.pre.exception.ServiceException;
import com.example.pre.model.AskOff;
import com.example.pre.tools.StringUtils;
import com.example.pre.tools.Tools;

/**
 * 请假controller接口
 * 
 * @author Administrator
 *
 */
@RestController
public class AskOffController extends ControllerBase {

	private final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private AskOffBusiness askOffBusiness;

	/**
	 * 创建请假申请
	 * 
	 * @param startTime 请假开始时间
	 * @param endTime   请假结束时间
	 * @param duration  请假时长
	 * @param type      请假类型
	 * @param content   请假事由
	 * @return
	 */
	@RequestMapping(value = "/askOff", method = RequestMethod.POST)
	public Map<String, Object> createAskOff(@RequestParam Long startTime, @RequestParam Long endTime,
			@RequestParam Integer duration, @RequestParam AskOffTypeEnum type, @RequestParam String content,
			@RequestParam(required = false) MultipartFile file) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数startTime=" + startTime + ", endTime=" + endTime + ", duration=" + duration
				+ ", type=" + type + ", content=" + content + ", file=" + file;
		Integer userId = this.getUserId();
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);

		try {
			// 参数验证
			if (startTime == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "开始时间不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (endTime == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "结束时间不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (duration == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "请假时长不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			if (StringUtils.isEmpty(content)) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "请假事由不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			AskOff askOff = askOffBusiness.createAskOff(userId, startTime, endTime, duration, type, content, file);
			result.put("data", askOff);
			// 存返回数据
			message = "创建请假申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建请假申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建请假申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "创建请假申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 下载附件
	 * 
	 * @param askOffId
	 * @return
	 */
	@RequestMapping(value = "/askOff/file", method = RequestMethod.GET)
	public Map<String, Object> downAskOff(@RequestParam Integer askOffId) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数askOffId=" + askOffId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);

		try {
			// 参数验证
			if (askOffId == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "开始时间不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}
			askOffBusiness.downAskOff(request, response, askOffId);
			// 存返回数据
			message = "创建请假申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建请假申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建请假申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "创建请假申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 修改请假申请
	 * 
	 * @param askOffId  请假申请id
	 * @param startTime 请假开始时间
	 * @param endTime   请假结束时间
	 * @param duration  请假时长
	 * @param type      请假类型
	 * @param content   请假事由
	 * @return
	 */
	@RequestMapping(value = "/askOff", method = RequestMethod.PUT)
	public Map<String, Object> updateAskOff(@RequestParam Integer askOffId,
			@RequestParam(required = false) Long startTime, @RequestParam(required = false) Long endTime,
			@RequestParam(required = false) Integer duration, @RequestParam(required = false) AskOffTypeEnum type,
			@RequestParam(required = false) String content, @RequestParam(required = false) MultipartFile file) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数,askOffId=" + askOffId + "startTime=" + startTime + ", endTime=" + endTime
				+ ", duration=" + duration + ", type=" + type + ", content=" + content;
		Integer userId = this.getUserId();
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);

		try {
			// 参数验证
			if (askOffId == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "请假申请id不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			askOffBusiness.updateAskOff(userId, askOffId, startTime, endTime, duration, type, content, file);
			// 存返回数据
			message = "修改请假申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建请假申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "创建请假申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "创建请假申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 查看请假申请
	 * 
	 * @param askOffId 请假申请id
	 * @return
	 */
	@RequestMapping(value = "/askOff", method = RequestMethod.GET)
	public Map<String, Object> checkAskOff(@RequestParam Integer askOffId) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数askOffId=" + askOffId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {
			// 参数验证
			if (askOffId == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "请假申请id不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

			AskOff askOff = askOffBusiness.checkAskOff(askOffId);
			result.put("data", askOff);
			// 存返回数据
			message = "查看请假申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "查看请假申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "查看请假申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "查看请假申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

	/**
	 * 发起请假申请
	 * 
	 * @param askOffId 请假申请id
	 * @return
	 */
	@RequestMapping(value = "/askOff/flow", method = RequestMethod.POST)
	public Map<String, Object> flowAskOff(@RequestParam Integer askOffId) {
		// 获取响应时间
		long start = System.currentTimeMillis();
		String eventId = Tools.createUUID();
		String message = "";
		String inputPara = ",传入的参数askOffId=" + askOffId;
		// 预创建返回值
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("flag", false);
		result.put("message", "OK");
		result.put("code", 0);
		result.put("data", null);
		try {
			// 参数验证
			if (askOffId == null) {
				result.put("message", "参数有误");
				result.put("code", -501);
				message = "请假申请id不能为空";
				logger.error(super.createLog(new Exception(), message + inputPara, start, eventId));
				return result;
			}

//									UserVO userVO = userBusiness.createUser(name, account, sex, password, departmentId, type, pic);
//									result.put("data", userVO);
			// 存返回数据
			message = "发起请假申请成功";
			result.put("flag", true);
			result.put("message", message);
			logger.info(super.createLog(message + inputPara, start, eventId));
			return result;
		} catch (ServiceException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "发起请假申请时，业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (ExtParaException e) {
			ExceptionMessage exceptionMessage = e.getExceptionMessage();
			int code = exceptionMessage.getCode();
			result.put("code", code);
			result.put("message", exceptionMessage.getShowMessage());
			message = "发起请假申请时，第三方业务报错：" + exceptionMessage.getLogMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", -400);
			result.put("message", "系统繁忙，请稍后重试");
			message = "发起请假申请时，系统报错：" + e.getMessage();
			logger.error(super.createLog(e, message + inputPara, start, eventId));
			return result;
		}
	}

}
